define(['vue','common','ELEMENT'],function (Vue,common,ELEMENT){
    const Controller = {
        index:function (){
            let {createApp} = Vue;
            const {ElLoading,ElMessage,ElMessageBox } = ELEMENT;
            let app = createApp({
                data(){
                    return {
                        //列表数据
                        listObj:{
                            page:1,
                            limit:10,
                            total:0,
                            list:[]
                        },
                        //搜索条件
                        search:{
                            list:[],
                            form:{
                                filter:[],
                                op:[],
                                list_type:"tree",
                                pk:"id",
                                sort:'id',
                                order:"desc"
                            }
                        },
                        //批量选择
                        selectables:[]
                    }
                },
                created(){
                    this.getList()
                },
                methods:{
                    ...common,
                    handleSwitchChange(row,field,pk='id'){
                        if(this.search?.form?.pk){
                            pk = this.search.form.pk
                        }
                        let newurl = common.getHandleUrl('multi')
                        let that  = this
                        this.post(newurl,{ids:row[pk],field:field}).then(res=>{
                            if(res.code==1){
                                that.success(res.msg,()=>{
                                    that.getList()
                                    parent.updateMenu()
                                })
                            }else{
                                that.error(res.msg,function (){
                                    that.getList()
                                })
                            }
                        })
                    },
                    handleAdd(area=['600px', '95%']){
                        let params =common.windowsGetParams()
                        let url = common.getHandleUrl('add')
                        if(params){
                            url += '?'+params
                        }
                        // console.log(url)
                        let that = this
                        common.open('添加',url,area,function (layero, index){
                            that.getList()
                            parent.updateMenu()
                        })
                    },
                    handleDelete(row,pk='id'){
                        if(this.search?.form?.pk){
                            pk = this.search.form.pk
                        }
                        let ids = ""
                        if((typeof row)=='string'||(typeof row)=='number'){
                            ids = row
                        }
                        if(Array.isArray(row)){
                            ids = row.map(item=>item[pk]).join(',')
                        }else if((typeof row)=='object'){
                            ids = row[pk]
                        }
                        if(!ids){
                            common.message('请选择要删除的项')
                            return
                        }
                        let that = this;
                        ElMessageBox({
                            title: '提示',
                            message: '确定要删除吗？',
                            type: 'warning',
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            showCancelButton:true,
                            callback: (action) => {
                                console.log(action)
                                if (action === 'confirm') {
                                    let url = `/app/${Config.plugin}/${Config.app}/${Config.controller}/del`
                                    common.postHandle(url,{ids:ids}).then(res=>{
                                        common.message('删除成功')
                                        // console.log("删除成功------------")
                                        that.getList();
                                        parent.updateMenu()
                                        // console.log(999999)
                                    })
                                }
                            }
                        })
                    }
                }
            })
            common.load(app,['icon'])
            app.mount('#app')
        },
        add:function (){
            let {createApp} = Vue;
            let app = createApp({
                data(){
                    return {
                        form:{
                            type:'dir',
                            pid:0,
                            name:'',
                            plugin:'',
                            module:'',
                            route:"",
                            title:"",
                            icon:'',
                            remark:"",
                            ismenu:1,
                            weigh:"",
                            status:1
                        },
                        rules:{
                            name: [
                                { required: true, message: '请输入名称', trigger: 'blur' }
                            ],
                            title: [
                                { required: true, message: '请输入名称', trigger: 'blur' }
                            ],
                            plugin:[
                                { required: true, message: '请选择插件', trigger: 'blur' }
                            ],
                            module:[
                                { required: true, message: '请选择模块', trigger: 'blur' }
                            ],
                            type:[
                                { required: true, message: '请选择类型', trigger: 'blur' }
                            ],
                            pid:[
                                { required: true, message: '请选择上级', trigger: 'blur' }
                            ],
                            route:[
                                { required: true, message: '请填写路由', trigger: 'blur' }
                            ]

                        },
                        options:{
                            typeList:[],//类型
                            pidList:[],//父级
                            pluginList:[],//插件
                            moduleList:[],//模块
                        }
                    }
                },
                created() {
                    this.options.typeList = Config.menu_type_list
                    this.options.pidList = Config.menu_list
                    this.options.pluginList = Config.plugin_list
                    // console.log(this.options,'ooooooooooooooooo')
                },
                methods: {
                    ...common,
                    handleSelectChange(e){
                        if(!e){
                            this.options.moduleList = []
                            return
                        }
                        let url = common.getHandleUrl('get_module')
                        let that = this
                        common.postHandle(url,{plugin:e}).then(res=>{
                            that.options.moduleList = res
                            res.map(item=>{
                                if(item.value=='admin'){
                                    that.form.module = 'admin'
                                }
                            })
                        })
                    }
                }
            })
            common.load(app,['icon','MuTreeSelect','MuIcon'])
            app.mount('#app')
        },
        edit:function (){
            let {createApp} = Vue;
            let app = createApp({
                data(){
                    return {
                        form:{
                            type:'dir',
                            pid:0,
                            name:'',
                            plugin:'',
                            module:'',
                            route:"",
                            title:"",
                            icon:'',
                            remark:"",
                            ismenu:1,
                            weigh:"",
                            status:1
                        },
                        rules:{
                            name: [
                                { required: true, message: '请输入名称', trigger: 'blur' }
                            ],
                            title: [
                                { required: true, message: '请输入名称', trigger: 'blur' }
                            ],
                            plugin:[
                                { required: true, message: '请选择插件', trigger: 'blur' }
                            ],
                            module:[
                                { required: true, message: '请选择模块', trigger: 'blur' }
                            ],
                            type:[
                                { required: true, message: '请选择类型', trigger: 'blur' }
                            ],
                            pid:[
                                { required: true, message: '请选择上级', trigger: 'blur' }
                            ],
                            route:[
                                { required: true, message: '请填写路由', trigger: 'blur' }
                            ]

                        },
                        options:{
                            typeList:[],//类型
                            pidList:[],//父级
                            pluginList:[],//插件
                            moduleList:[],//模块
                        }
                    }
                },
                created() {
                    this.options.typeList = Config.menu_type_list
                    this.options.pidList = Config.menu_list
                    this.options.pluginList = Config.plugin_list
                    this.form = common.addObject(this.form,Config.row)
                    if(this.form.plugin){
                        this.handleSelectChange(this.form.plugin)
                    }
                    // console.log(Config,'ooooooooooooooooo')
                },
                methods: {
                    ...common,
                    // getSelectChange(plugin){
                    //     common.postHandle(url,{plugin:plugin}).then(res=>{
                    //         that.options.moduleList = res
                    //         res.map(item=>{
                    //             if(item.value=='admin'){
                    //                 that.form.module = 'admin'
                    //             }
                    //         })
                    //     })
                    // },
                    handleSelectChange(e){
                        if(!e){
                            this.options.moduleList = []
                            return
                        }
                        // return
                        let url = common.getHandleUrl('get_module')
                        let that = this
                        common.postHandle(url,{plugin:e}).then(res=>{
                            that.options.moduleList = res
                            res.map(item=>{
                                if(item.value=='admin'){
                                    that.form.module = 'admin'
                                }
                            })
                        })
                    }
                }
            })
            common.load(app,['icon','MuTreeSelect','MuIcon'])
            app.mount('#app')
        }
    }
    return Controller;
})